package com.google.coffeeshop.business;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import vn.bang.utility.JpaUtil;

import com.google.coffeeshop.dao.DAOFactory;
import com.google.coffeeshop.entity.ProductDTO;
import com.google.coffeeshop.entity.ReceiptNoteDTO;
import com.google.coffeeshop.entity.ReceiptNoteLineDTO;
import com.google.coffeeshop.log.Log;

public class AvailabilityBusiness extends AbstractBaseBusiness {

	static public List<ReceiptNoteDTO> findAllReceiptNotes() {
		List<ReceiptNoteDTO> receiptNote = new ArrayList<ReceiptNoteDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			receiptNote.addAll(DAOFactory.createReceiptNoteDAO(entityManager).findAll());
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return receiptNote;
	}
	
	static public List<ReceiptNoteLineDTO> findReceiptNoteLinesByReceiptNoteId(long receiptNoteId) {
		List<ReceiptNoteLineDTO> receiptNoteLines = new ArrayList<ReceiptNoteLineDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			receiptNoteLines.addAll(DAOFactory.createReceiptNoteLineDAO(entityManager).findByReceiptNoteId(receiptNoteId));
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return receiptNoteLines;
	}
	
	static public ProductDTO findProductById(long id) {
		ProductDTO productDTO = null;
		final EntityManager entityManager = createEntityManager();
		try {
			productDTO = DAOFactory.createProductDAO(entityManager).find(id);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return productDTO;
	}
	
	static public void saveReceiptNote(ReceiptNoteDTO receiptNote) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
				if (receiptNote != null) {
					correctReceiptNoteAmount(receiptNote);
					DAOFactory.createReceiptNoteDAO(entityManager).save(receiptNote);
				}
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	static public void warehousingProducts(ReceiptNoteDTO receiptNote) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			if (receiptNote != null && !receiptNote.getReceiptNoteLines().isEmpty()) {
				for (ReceiptNoteLineDTO receiptNoteLine : receiptNote.getReceiptNoteLines()) {
					warehousingProduct(receiptNoteLine.getProduct(), receiptNoteLine.getQuantity());
					DAOFactory.createProductDAO(entityManager).save(receiptNoteLine.getProduct());
				}
			}
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	private static void warehousingProduct(ProductDTO product, long quantity) {
		product.setInventory(product.getInventory() + quantity);
	}
	
	static public void correctReceiptNoteAmount(ReceiptNoteDTO receiptNote) {
		if (receiptNote != null && !receiptNote.getReceiptNoteLines().isEmpty()) {
			long totalAmount = 0;
			for (ReceiptNoteLineDTO receiptLine : receiptNote.getReceiptNoteLines()) {
				totalAmount += receiptLine.getPriceOfUnit() * receiptLine.getQuantity();
			}
			receiptNote.setAmount(totalAmount);
		}
	}
	
}
